package com.yunmang.activity;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.yunmang.R;
import com.yunmang.app.AppConstant;
import com.yunmang.fragment.BlackBordFragment;
import com.yunmang.fragment.HomePageFragment;
import com.yunmang.fragment.ServiceFragment;
import com.yunmang.fragment.SettingFragment;
import com.yunmang.fragment.StatementFragment;
import com.yunmang.utils.SharedPreferencesUtils;

import de.greenrobot.event.EventBus;

/**
 * 作者 : bean on 2017/4/5/0005.
 * 注释 :
 */
public class HomePageActivity extends BaseActivity {
    private BottomNavigationBar bottomNavigationBar;
    int lastSelectedPosition = 0;
    private ServiceFragment serviceFragment;
    private HomePageFragment homePageFragment;
    private StatementFragment statementFragment;
    private SettingFragment settingFragment;
    private BlackBordFragment blackBordFragment;
    private long exitTime = 0;
    @Override
    public int getLayoutId() {
        return R.layout.activity_home_page;
    }

    @Override
    public void initView() {
        BottomNavigationBar bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.mipmap.icon_sy_tab_sy_pre, "首页").setActiveColorResource(R.color.blue))
                .addItem(new BottomNavigationItem(R.mipmap.icon_sy_tab_yy_pre, "看板").setActiveColorResource(R.color.blue))
                .addItem(new BottomNavigationItem(R.mipmap.icon_sy_tab_bb_pre, "营运").setActiveColorResource(R.color.blue))
                .addItem(new BottomNavigationItem(R.mipmap.icon_sy_tab_bao_pre, "报表").setActiveColorResource(R.color.blue))
                .addItem(new BottomNavigationItem(R.mipmap.icon_sy_tab_sz_pre, "设置").setActiveColorResource(R.color.blue))
                .setFirstSelectedPosition(lastSelectedPosition)
                .initialise();
        bottomNavigationBar.setTabSelectedListener(new MyOnTabSelected());
        setDefaultFragment();
    }

    private void setDefaultFragment() {
        android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
        android.support.v4.app.FragmentTransaction transaction = fm.beginTransaction();
        serviceFragment = new ServiceFragment();
        transaction.replace(R.id.layFrame, serviceFragment);
        transaction.commit();

    }


    @Override
    public void initData() {
        //登录成功之后，可以初始化appconstant
//        String isAuto_transport=SharedPreferencesUtils.getParameter(context,"isAutoUpload_transport");
//        String isAll_transport=SharedPreferencesUtils.getParameter(context,"isAllScan_transport");
//        if(isAuto_transport.equals("true")){
//            AppConstant.isAutoUpload_transport=true;
//        }else{
//            AppConstant.isAutoUpload_transport=false;
//        }
//        if(isAll_transport.equals("true")){
//            AppConstant.isAllScan_transport=true;
//        }else{
//            AppConstant.isAllScan_transport=false;
//        }
    }

    android.support.v4.app.FragmentManager fm = this.getSupportFragmentManager();

    private class MyOnTabSelected implements BottomNavigationBar.OnTabSelectedListener {
        @Override
        public void onTabSelected(int position) {
            android.support.v4.app.FragmentTransaction transaction = fm.beginTransaction();
            //开启事务
            switch (position) {
                case 0:
                    if (serviceFragment == null) {
                        serviceFragment = new ServiceFragment();
                    }
                    transaction .replace(R.id.layFrame, serviceFragment);
                    break;
                case 1:
                    if (blackBordFragment == null) {
                        blackBordFragment = new BlackBordFragment();
                    }
                    transaction.replace(R.id.layFrame, blackBordFragment);
                    break;
                case 2:

                    if (homePageFragment == null) {
                        homePageFragment = new HomePageFragment();
                    }
                    transaction.replace(R.id.layFrame, homePageFragment);
                    break;
                case 3:
//                    if (blackBordFragment == null) {
//                        blackBordFragment = new BlackBordFragment();
//                    }
//                    transaction.replace(R.id.layFrame, blackBordFragment);
                    if (statementFragment == null) {
                        statementFragment = new StatementFragment();
                    }
                    transaction.replace(R.id.layFrame, statementFragment);
                    break;
                case 4:
                    if (settingFragment == null) {
                        settingFragment = new SettingFragment();
                    }
                    transaction.replace(R.id.layFrame, settingFragment);
                    break;
                default:
                    break;
            }
            // 事务提交
            transaction.commit();

        }

        @Override
        public void onTabUnselected(int position) {

        }

        @Override
        public void onTabReselected(int position) {

        }
    }

    @Override
    public void onBackPressed() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            showToast("再点一次退出程序");
            exitTime = System.currentTimeMillis();
        } else {
            finish();
        }
    }
}
